<?php

/**
 * iwebform
 * Lightweight controller-view structure library for PHP
 * @author Talha Buğra Bulut talhabulut@hotmail.com
 */

define('VIEW_PATH', APPLICATION_PATH . 'views/');
define('LAYOUT_PATH', 'layouts/'); // must be under view path
define('PARTIAL_PATH', 'partials/'); // must be under view path
define('HELPER_PATH', realpath(dirname(__FILE__)) . "/helpers");
define('PAGE_404', APPLICATION_PATH . 'template/error/404.html');

class iwebform {

    protected $case_key = 'r';
    protected $actions = array();
    protected $view_path = VIEW_PATH;
    protected $layout_path = LAYOUT_PATH;
    protected $partial_path = PARTIAL_PATH;
    protected $page_404 = PAGE_404;
    protected $layout = "default.phtml";
    protected $helper;

    /**
     * Constructor
     */
    public function __construct() {
        require_once 'ihelper.php';
        $this->helper = new ihelper();
    }

    /**
     * Sets view path
     * @param string $val
     */
    public function set_view_path($val) {
        $this->view_path = $val;
    }

    /**
     * Sets layout path
     * @param string $val
     */
    public function set_layout_path($val) {
        $this->layout_path = $val;
    }

    /**
     * Sets partial path
     * @param unknown_type $val
     */
    public function set_partial_path($val) {
        $this->partial_path = $val;
    }

    /**
     * Sets layout
     * @param string $val
     */
    public function set_layout($val) {
        $this->layout = $val;
    }

    /**
     * Sets 404 error page
     * @param string $val
     */
    public function set_page_404($val) {
        $this->page_404 = $val;
    }

    /**
     * Adds action
     * @param string $key
     * @param string $actionfunc
     */
    public function add_action($key, $actionfunc) {
        $this->actions[$key] = array('func' => $actionfunc);
    }

    /**
     * Removes action
     * @param unknown_type $key
     */
    public function remove_action($key) {
        unset($this->actions[$key]);
    }

    /**
     * Views file
     * @param string $file
     * @param array $vars
     * @param boolean $in_layout
     */
    protected function view($file, $vars=array(), $in_layout=true) {
        if ($in_layout) {
            $vars['__view'] = $file;
            $vars['__vars'] = $vars;
            $this->view($this->layout_path . $this->layout, $vars, false);
        } else {
            $this->view_file($file, $vars);
        }
    }

    /**
     * Views file
     * @param string $file
     * @param array $vars
     */
    protected function view_file($file, $vars=array()) {
        if (is_array($vars)) {
            $_var_keys = array_keys($vars);
            foreach ($_var_keys as $_key) {
                $$_key = $vars[$_key];
            }
        }
        require($this->view_path . $file);
    }

    /**
     * Views partial file
     * @param string $file
     * @param array $vars
     */
    protected function partial($file, $vars=array()) {
        $this->view_file($this->partial_path . $file, $vars);
    }

    /**
     * Handles actions
     */
    public function handle() {
        $requested_action = null;
        if (isset($_GET[$this->case_key])) {
            $requested_action = $_GET[$this->case_key];
        }

        if (array_key_exists($requested_action, $this->actions)) {
            $action = $this->actions[$requested_action];
            $func = $action['func'];
            $this->$func();
        } else {
            // action not found (404 error)
            $this->error_404();
        }
    }

    /**
     * Request Object
     * @return request
     */
    public function request() {
        require_once 'irequest.php';
        return new irequest();
    }

    /**
     * 404 HTTP Error
     */
    protected function error_404() {
        ob_clean();
        header("HTTP/1.0 404 Not Found");
        header("Status: 404 Not Found");
        if (file_exists($this->page_404)) {
            echo file_get_contents($this->page_404);
        } else {
            echo "<h1>404</h1>PAGE NOT FOUND";
        }
        exit();
    }

}

?>
